package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Actual_Form_BaoYangHuiZhi extends BaseActivity {
	private Spinner spinner_jixiemingcheng;
	private PictureButton PictureButton_WaiGuan,PictureButton_JiaShiChi,PictureButton_ShuiXiang,PictureButton_DianPing,picturebutton_huangyou;
	private PictureButton PictureButton_WaiGuan_Left, pictureButton_WaiGuan_Right, PictureButton_ShuiXiang_Left, PictureButton_ShuiXiang_Right;

	@Override
	public void onClick(View view) {
		super.onClick(view);
		if (view.getId()== R.id.editText_to_baoyanglist) {
			Intent intent = new Intent(Actual_Form_BaoYangHuiZhi.this, Actual_Form_BaoYang.class);
			startActivity(intent);
		}
	}
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actual_form_baoyanghuizhi);
		InitControls();
	}

	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1; mPrintenabled = 0;
			JsonMyID();
			getTask();
			getDataExt();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			save_pic();
		}catch (Exception e) {
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
//		if (PictureButton_WaiGuan!=null) {
//			PictureButton_WaiGuan.postInvalidate();
//		}
//		if (PictureButton_WaiGuan_Left!=null) {
//			PictureButton_WaiGuan_Left.postInvalidate();
//		}
//		if (pictureButton_WaiGuan_Right!=null) {
//			pictureButton_WaiGuan_Right.postInvalidate();
//		}
//		
//		if (PictureButton_JiaShiChi!=null) {
//			PictureButton_JiaShiChi.postInvalidate();
//		}
//		if (PictureButton_ShuiXiang!=null) {
//			PictureButton_ShuiXiang.postInvalidate();
//		}
//		if (PictureButton_ShuiXiang_Left!=null) {
//			PictureButton_ShuiXiang_Left.postInvalidate();
//		}
//		if (PictureButton_ShuiXiang_Right!=null) {
//			PictureButton_ShuiXiang_Right.postInvalidate();
//		}
//		if (PictureButton_DianPing!=null) {
//			PictureButton_DianPing.postInvalidate();
//		}
//		if (picturebutton_huangyou!=null) {
//			picturebutton_huangyou.postInvalidate();
//		}
	}
	private void save_pic() throws JSONException {
		if (PictureButton_WaiGuan!=null) {
			mJson.put( PictureButton_WaiGuan.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_WaiGuan.mFileName);
		}
		if (PictureButton_WaiGuan_Left!=null) {
			mJson.put( PictureButton_WaiGuan_Left.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_WaiGuan_Left.mFileName);
		}
		if (pictureButton_WaiGuan_Right!=null) {
			mJson.put( pictureButton_WaiGuan_Right.FIELD+getString(R.string.title_actual_form_PictureButton), pictureButton_WaiGuan_Right.mFileName);
		}
		
		if (PictureButton_JiaShiChi!=null) {
			mJson.put( PictureButton_JiaShiChi.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_JiaShiChi.mFileName);
		}
		
		if (PictureButton_ShuiXiang!=null) {
			mJson.put( PictureButton_ShuiXiang.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_ShuiXiang.mFileName);
		}
		if (PictureButton_ShuiXiang_Left!=null) {
			mJson.put( PictureButton_ShuiXiang_Left.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_ShuiXiang_Left.mFileName);
		}
		if (PictureButton_ShuiXiang_Right!=null) {
			mJson.put( PictureButton_ShuiXiang_Right.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_ShuiXiang_Right.mFileName);
		}
		
		if (PictureButton_DianPing!=null) {
			mJson.put( PictureButton_DianPing.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_DianPing.mFileName);
		}			
		if (picturebutton_huangyou!=null) {
			mJson.put( picturebutton_huangyou.FIELD+getString(R.string.title_actual_form_PictureButton), picturebutton_huangyou.mFileName);
		}		
	}
	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			if (PictureButton_WaiGuan!=null) {
				CopyPicDest(PictureButton_WaiGuan, sPath);
			}
			if (PictureButton_WaiGuan_Left!=null) {
				CopyPicDest(PictureButton_WaiGuan_Left, sPath);
			}
			if (pictureButton_WaiGuan_Right!=null) {
				CopyPicDest(pictureButton_WaiGuan_Right, sPath);
			}
			if (PictureButton_JiaShiChi!=null) {
				CopyPicDest(PictureButton_JiaShiChi, sPath);
			}
			if (PictureButton_ShuiXiang!=null) {
				CopyPicDest(PictureButton_ShuiXiang, sPath);
			}
			if (PictureButton_ShuiXiang_Left!=null) {
				CopyPicDest(PictureButton_ShuiXiang_Left, sPath);
			}
			if (PictureButton_ShuiXiang_Right!=null) {
				CopyPicDest(PictureButton_ShuiXiang_Right, sPath);
			}
			if (PictureButton_DianPing!=null) {
				CopyPicDest(PictureButton_DianPing, sPath);
			}			
			if (picturebutton_huangyou!=null) {
				CopyPicDest(picturebutton_huangyou, sPath);
			}				
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	protected void getDataExt() throws JSONException {
		if (spinner_jixiemingcheng!=null) {
			mJson.put( getString(R.string.title_actual_form_edittext_jixiemingcheng), 
					spinner_jixiemingcheng.getSelectedItem().toString());
		}
		if (editText_driver!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_driver), editText_driver.getText().toString());
		}
		if (Actual_Form_BaoYang.Instance.mValue!=null) {
			mJson.put( "保养详单:", Actual_Form_BaoYang.Instance.mValue);
		}
	}
	
	@Override
	public void InitControls() {
		super.InitControls();
		spinner_jixiemingcheng = (Spinner) findViewById(R.id.spinner_jixiemingcheng);
		
		PictureButton_WaiGuan =(PictureButton)findViewById(R.id.picturebutton_waiguan);
		PictureButton_JiaShiChi =(PictureButton)findViewById(R.id.picturebutton_jiashichi);
		PictureButton_ShuiXiang=(PictureButton)findViewById(R.id.picturebutton_shuixiang);
		PictureButton_DianPing=(PictureButton)findViewById(R.id.picturebutton_dianping);
		picturebutton_huangyou=(PictureButton)findViewById(R.id.picturebutton_huangyou);
		
//		PictureButton_WaiGuan_Left =(PictureButton)findViewById(R.id.picturebutton_waiguan_left);
//		pictureButton_WaiGuan_Right =(PictureButton)findViewById(R.id.picturebutton_waiguan_right);
		PictureButton_ShuiXiang_Left=(PictureButton)findViewById(R.id.picturebutton_shuixiang_left);
		PictureButton_ShuiXiang_Right=(PictureButton)findViewById(R.id.picturebutton_shuixiang_right);
		
		if (spinner_jixiemingcheng!=null) {
			try {
				String T_JIXIE = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.
						getJSONObject(Submit_System_Setting_Data_Task.mID).getString("WaiZuJiXie");
				String[] list = T_JIXIE.split("\r\n");
				if (list.length>0) {
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					R.layout.simple_spinner_item, 
					list);
					adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
					spinner_jixiemingcheng.setAdapter(adapter);
				}else {
					Utility.MessageBox(this,"机械设备名称数据没有下载");
					this.finish();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			if (Submit_System_Setting_Data_Task.A_ZIYOUJIXIE!=null
//					|| Submit_System_Setting_Data_Task.A_WAIZUJIXIE!=null
//					|| Submit_System_Setting_Data_Task.A_ZIYOUSHILUN!=null) {
//				//String[] mingcheng = new string
//				ArrayList<String> list = new ArrayList<String>();
//				int i =0 ; 
//				if (Submit_System_Setting_Data_Task.A_ZIYOUJIXIE!=null) {
//					for (i = 0; i < Submit_System_Setting_Data_Task.A_ZIYOUJIXIE.length; i++) {
//						list.add(Submit_System_Setting_Data_Task.A_ZIYOUJIXIE[i]);
//					}
//				}
//				if (Submit_System_Setting_Data_Task.A_WAIZUJIXIE!=null) {
//					for (i = 0; i < Submit_System_Setting_Data_Task.A_WAIZUJIXIE.length; i++) {
//						list.add(Submit_System_Setting_Data_Task.A_WAIZUJIXIE[i]);
//					}
//				}
//				if (Submit_System_Setting_Data_Task.A_ZIYOUSHILUN!=null) {
//					for (i = 0; i < Submit_System_Setting_Data_Task.A_ZIYOUSHILUN.length; i++) {
//						list.add(Submit_System_Setting_Data_Task.A_ZIYOUSHILUN[i]);
//					}
//				}				
//				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//						R.layout.simple_spinner_item, 
//						list);
//				adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
//				spinner_jixiemingcheng.setAdapter(adapter);
		
		}
	}
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			if ( Actual_Form_BaoYang.Instance==null || Actual_Form_BaoYang.Instance.mValue.equals("")) {
				Utility.MessageBox(this,"保养详单没有填写!");
				return false;
			}
			if (!CheckEditEmpty(editText_driver)) {
				return false;
			}
			if (PictureButton_WaiGuan!=null && !PictureButton_WaiGuan.IsExist()) {
				Utility.MessageBox(this, PictureButton_WaiGuan.FIELD );
				return false;
			}
			if (PictureButton_WaiGuan_Left!=null && !PictureButton_WaiGuan_Left.IsExist()) {
				Utility.MessageBox(this, PictureButton_WaiGuan_Left.FIELD );
				return false;
			}
			if (pictureButton_WaiGuan_Right!=null && !pictureButton_WaiGuan_Right.IsExist()) {
				Utility.MessageBox(this, pictureButton_WaiGuan_Right.FIELD );
				return false;
			}
			
			if (PictureButton_JiaShiChi!=null && !PictureButton_JiaShiChi.IsExist()) {
				Utility.MessageBox( this,PictureButton_JiaShiChi.FIELD );
				return false;
			}
			
			if (PictureButton_ShuiXiang!=null && !PictureButton_ShuiXiang.IsExist()) {
				Utility.MessageBox( this,PictureButton_ShuiXiang.FIELD );
				return false;
			}
			if (PictureButton_ShuiXiang_Left!=null && !PictureButton_ShuiXiang_Left.IsExist()) {
				Utility.MessageBox(this, PictureButton_ShuiXiang_Left.FIELD );
				return false;
			}
			if (PictureButton_ShuiXiang_Right!=null && !PictureButton_ShuiXiang_Right.IsExist()) {
				Utility.MessageBox(this, PictureButton_ShuiXiang_Right.FIELD );
				return false;
			}
			
			if (PictureButton_DianPing!=null && !PictureButton_DianPing.IsExist()) {
				Utility.MessageBox(this, PictureButton_DianPing.FIELD );
				return false;
			}			
			if (picturebutton_huangyou!=null && !picturebutton_huangyou.IsExist()) {
				Utility.MessageBox( this,picturebutton_huangyou.FIELD );
				return false;
			}			
			return true;
		}else {
			return false;
		}
	}

}
